package com.weighttracker.android;

import com.bappi.custom.activities.CustomActivity;
import com.bappi.utils.Constants;
import com.bappi.utils.DatabaseAccessor;
import com.bappi.utils.ImageUtils;
import com.bappi.utils.Utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Tab1Activity1 extends CustomActivity {

	private static final int TYPE_CALORIES = 0;
	private static final int TYPE_UNITS = 1;
	private static final int TYPE_ACTIVITIES = 2;
	private static final int TYPE_WEIGHT = 3;

	private ImageView calendarView;
	private Bitmap calImage;

	private ProgressBar caloriesProgressBar, unitProgressBar,
			activityProgressBar;
	private TextView caloriesStatusView, unitStatusView, activityStatusView;
	private TextView caloriesMaxValueView, unitMaxValueView,
			activityMaxValueView;

	private int caloriesMaxValue, unitMaxValue, activityMaxValue;
	private int caloriesCurValue, unitCurValue, activityCurValue;

	private SharedPreferences sharedPreference;
	private OnSharedPreferenceChangeListener ospcl;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.layout_tab_1_screen_1);		
//			Log.i("DREG", Math.round(5.49)+"/"+Math.round(5.50)+"/"+Math.round(5.51));
			sharedPreference = PreferenceManager
					.getDefaultSharedPreferences(getParent());
			ospcl = new OnSharedPreferenceChangeListener() {

				public void onSharedPreferenceChanged(
						SharedPreferences sharedPreferences, String key) {
					try {
						if (Constants.KEY_FOOD_CALORIES.equals(key)) {
							caloriesMaxValue = sharedPreference.getInt(
									Constants.KEY_FOOD_CALORIES, 0);
							runOnUiThread(new Runnable() {

								@Override
								public void run() {
									try {
										setCaloriesViews();
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
							});
						} else if (Constants.KEY_FOOD_UNITS.equals(key)) {
							unitMaxValue = sharedPreference.getInt(
									Constants.KEY_FOOD_UNITS, 0);
							runOnUiThread(new Runnable() {

								@Override
								public void run() {
									try {
										setUnitsViews();
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
							});
						} else if (Constants.KEY_ACTIVITIES_WORKOUT.equals(key)) {
							activityMaxValue = sharedPreference.getInt(
									Constants.KEY_ACTIVITIES_WORKOUT, 0);
							runOnUiThread(new Runnable() {

								@Override
								public void run() {
									try {
										setActivityViews();
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
							});
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
			calendarView = (ImageView) findViewById(R.id.calendar_image_view);
			calImage = ImageUtils.getCalendarImage(getResources(),Utils.getDPIFactor(getParent()));
			calendarView.setImageBitmap(calImage);
			caloriesProgressBar = (ProgressBar) findViewById(R.id.calories_progress_bar);
			unitProgressBar = (ProgressBar) findViewById(R.id.units_progress_bar);
			activityProgressBar = (ProgressBar) findViewById(R.id.workout_activities_progress_bar);
			caloriesStatusView = (TextView) findViewById(R.id.calories_status_view);
			unitStatusView = (TextView) findViewById(R.id.unit_status_view);
			activityStatusView = (TextView) findViewById(R.id.workout_activities_status_view);

			caloriesMaxValueView = (TextView) findViewById(R.id.calories_progress_bar_max_value);
			unitMaxValueView = (TextView) findViewById(R.id.units_progress_bar_max_value);
			activityMaxValueView = (TextView) findViewById(R.id.workout_activities_progress_bar_max_value);

			findViewById(R.id.button_log_calories_ate_edit).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							if (caloriesMaxValue > 0) {
								setOrAddValue(TYPE_CALORIES, false);
							} else {
								Utils.showAlertMessage(
										getParent(),
										getString(R.string.message_set_up_goal_for_food_calories));
							}
						}
					});
			findViewById(R.id.button_log_calories_ate).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							if (caloriesMaxValue > 0) {
								setOrAddValue(TYPE_CALORIES, true);
							} else {
								Utils.showAlertMessage(
										getParent(),
										getString(R.string.message_set_up_goal_for_food_calories));
							}
						}
					});

			findViewById(R.id.button_log_units_ate_edit).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							if (unitMaxValue > 0) {
								setOrAddValue(TYPE_UNITS, false);
							} else {
								Utils.showAlertMessage(
										getParent(),
										getString(R.string.message_set_up_goal_for_food_units));
							}
						}
					});
			findViewById(R.id.button_log_units_ate).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							if (unitMaxValue > 0) {
								setOrAddValue(TYPE_UNITS, true);
							} else {
								Utils.showAlertMessage(
										getParent(),
										getString(R.string.message_set_up_goal_for_food_units));
							}
						}
					});

			findViewById(R.id.button_log_calories_burned_edit)
					.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							setOrAddValue(TYPE_ACTIVITIES, false);
						}
					});
			findViewById(R.id.button_log_calories_burned).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							setOrAddValue(TYPE_ACTIVITIES, true);
						}
					});
			findViewById(R.id.button_log_your_weight).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							setOrAddValue(TYPE_WEIGHT, true);
						}
					});

			caloriesMaxValue = sharedPreference.getInt(
					Constants.KEY_FOOD_CALORIES, 0);
			unitMaxValue = sharedPreference.getInt(Constants.KEY_FOOD_UNITS, 0);
			activityMaxValue = sharedPreference.getInt(
					Constants.KEY_ACTIVITIES_WORKOUT, 0);
			caloriesCurValue = DatabaseAccessor.getCaloriesValue();
			unitCurValue = DatabaseAccessor.getUnitValue();
			activityCurValue = DatabaseAccessor.getActivitiesValue();

			setCaloriesViews();
			setUnitsViews();
			setActivityViews();

			sharedPreference.registerOnSharedPreferenceChangeListener(ospcl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setCaloriesViews() throws Exception {
		caloriesProgressBar.setMax(caloriesMaxValue);
		caloriesProgressBar.setProgress(caloriesCurValue);

		caloriesStatusView.setText(caloriesCurValue + " / "
				+ Math.max(0, caloriesMaxValue - caloriesCurValue));
		caloriesMaxValueView.setText(Integer.toString(caloriesMaxValue));
	}

	private void setUnitsViews() throws Exception {
		unitProgressBar.setMax(unitMaxValue);
		unitProgressBar.setProgress(unitCurValue);

		unitStatusView.setText(unitCurValue + " / "
				+ Math.max(0, unitMaxValue - unitCurValue));
		unitMaxValueView.setText(Integer.toString(unitMaxValue));
	}

	private void setActivityViews() throws Exception {
		activityProgressBar.setMax(activityMaxValue);
		activityProgressBar.setProgress(activityCurValue);

		activityStatusView.setText(activityCurValue + " / "
				+ Math.max(0, activityMaxValue - activityCurValue));
		activityMaxValueView.setText(Integer.toString(activityMaxValue));
	}

	@Override
	protected void onDestroy() {
		if (sharedPreference != null && ospcl != null) {
			sharedPreference.unregisterOnSharedPreferenceChangeListener(ospcl);
		}
		super.onDestroy();
	}

	private void setOrAddValue(final int type, final boolean isAdd) {
		try {
			final Dialog dialog = new Dialog(getParent());

			dialog.setContentView(LayoutInflater.from(getParent()).inflate(
					R.layout.input_dialog, null));
			dialog.getWindow().setLayout(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT);
			if (isAdd) {
				dialog.setTitle(getString(R.string.log));
			} else {
				dialog.setTitle(getString(R.string.edit));
			}
			TextView rightTextView = (TextView) dialog
					.findViewById(R.id.right_text_view);
			final EditText inputField = (EditText) dialog
					.findViewById(R.id.input_field);
			if (type == TYPE_CALORIES) {
				rightTextView.setText(getString(R.string.calories));
			} else if (type == TYPE_UNITS) {
				rightTextView.setText(getString(R.string.units));
			} else if (type == TYPE_ACTIVITIES) {
				rightTextView.setText(getString(R.string.calories_or_units));
			} else if (type == TYPE_WEIGHT) {
				rightTextView.setText(getString(R.string.lbs_or_kgs));
			}

			ImageView imageView = (ImageView) dialog
					.findViewById(R.id.calendar_image_view);
			imageView.setImageBitmap(calImage);
			// rightTextView.setText();
			Button okButton = (Button) dialog.findViewById(R.id.button_ok);
			Button cancelButton = (Button) dialog
					.findViewById(R.id.button_cancel);

			okButton.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					// startPaypalActivity();
					try {
						String text = inputField.getText().toString();
						int val = 0;
						if (text.length() > 0) {
							val = Integer.parseInt(text);
						}
						if (isAdd) {
							if (type == TYPE_CALORIES) {
								caloriesCurValue += val;
								DatabaseAccessor.saveCalories(caloriesCurValue);
								setCaloriesViews();
							} else if (type == TYPE_UNITS) {
								unitCurValue += val;
								DatabaseAccessor.saveUnit(unitCurValue);
								setUnitsViews();
							} else if (type == TYPE_ACTIVITIES) {
								activityCurValue += val;
								DatabaseAccessor
										.saveActivities(activityCurValue);
								setActivityViews();
							} else if (type == TYPE_WEIGHT) {
								DatabaseAccessor.saveWeight(val);
							}
						} else {
							if (type == TYPE_CALORIES) {
								caloriesCurValue = val;
								DatabaseAccessor.saveCalories(caloriesCurValue);
								setCaloriesViews();
							} else if (type == TYPE_UNITS) {
								unitCurValue = val;
								DatabaseAccessor.saveUnit(unitCurValue);
								setUnitsViews();
							} else if (type == TYPE_ACTIVITIES) {
								activityCurValue = val;
								DatabaseAccessor
										.saveActivities(activityCurValue);
								setActivityViews();
							} else if (type == TYPE_WEIGHT) {
								DatabaseAccessor.saveWeight(val);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}

					dialog.dismiss();
				}
			});
			cancelButton.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					try {
						dialog.dismiss();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}